import { Component, OnInit } from '@angular/core';
import { RecycleModelsService } from './recycle-models.service';

@Component({
    selector: 'app-recycle-models',
    templateUrl: './recycle-models.component.html',
    styleUrls: ['./recycle-models.component.scss']
})
export class RecycleModelsComponent implements OnInit {

    /**
     * 数据源
     */
    listOfData?: any;
    /**
     * 当前是否在搜索
     */
    searchHint: boolean = false;
    /**
     * 当前搜索的name
     */
    searchName: string = "";
    /**
     * 表格加载
     */
    tableLoading: boolean = false;
    queryForm: any = {
        // 品牌ID
        brandName: '',
        // 机型名称
        name: '',
        // 类型 1：手机 2：平板
        type: '',
        // 每页多少条
        pageSize: 20,
        // 第几页
        pageNum: 1
    }
    /**
     * 加载失败显示图像占位符
     */
    fallback =
        '';
    constructor(
        private recycleModelsService: RecycleModelsService
    ) { }

    ngOnInit() {
        this.getModelsList();
    }

    // 查询
    query() {
        if (this.queryForm.brandName != "" || this.queryForm.name != "" || this.queryForm.type != "") {
            this.searchHint = true;
        } else {
            this.searchHint = false;
        }

        this.getModelsList();
    }

    // 返回原列表
    backList() {
        this.queryForm.brandName = "";
        this.queryForm.name = "";
        this.queryForm.type = "";
        this.queryForm.pageNum = 1;
        this.searchHint = false;
        this.getModelsList();
    }

    /**
     * 重置
     */
     reset() {
        this.queryForm.brandName = "";
        this.queryForm.name = "";
        this.queryForm.type = "";
        this.queryForm.pageNum = 1;
        this.searchHint = false;
        this.getModelsList();
     }

    getModelsList() {
        this.recycleModelsService.getList(this.queryForm).subscribe((res: any) => {
            this.listOfData = res.data;
        })
    }

    onPageIndexChange(index: number) {
        console.log(index);
        this.queryForm.pageNum = index;
        this.getModelsList();
    }

    onPageSizeChange(index: number) {
        console.log(index);
        this.queryForm.pageSize = index;
        this.getModelsList();
    }
}
